//
//  nxzRegisterViewController.m
//  projectAlcohol
//
//  Created by Anand Rao on 11/28/13.
//  Copyright (c) 2013 home. All rights reserved.
//

#import "nxzRegisterViewController.h"

@interface nxzRegisterViewController ()

@end

@implementation nxzRegisterViewController

@synthesize loginUsernameField=_loginUsernameField, loginPasswordField=_loginPasswordField;
@synthesize responseData = _responseData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    _loginPasswordField.delegate = self;
    _loginUsernameField.delegate = self;
    _usernameField.delegate = self;
    _passwordField.delegate = self;
    self.responseData = [NSMutableData data];
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField) {
        [textField resignFirstResponder];
    }
    return NO;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [_usernameField resignFirstResponder];
    [_passwordField resignFirstResponder];
    [_loginUsernameField resignFirstResponder];
    [_loginPasswordField resignFirstResponder];
}


- (IBAction)registerAction:(id)sender {
    [_usernameField resignFirstResponder];
    [_emailField resignFirstResponder];
    [_passwordField resignFirstResponder];
    [_verifyPasswordField resignFirstResponder];
    [self checkFields];
}

- (IBAction)login:(id)sender {

    
    NSString *strURL = [NSString stringWithFormat:@"http://localhost/project-alcohol/index.php/%@",@"login"];
    
    NSString *bodyData = [NSString stringWithFormat:@"&username=%@&password=%@",_loginUsernameField.text, _loginPasswordField.text];
    NSLog(@"%@",bodyData);
    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL]];
    [postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [postRequest setHTTPMethod:@"POST"];
    [postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];
    
    //calling line
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];
    
    NSLog(@"%@",connection);
    NSLog(@"%@",bodyData);
    
    if (!connection) {
        NSLog(@"Connection Failed");
    }
    
    [_loginPasswordField resignFirstResponder];
    [_loginUsernameField resignFirstResponder];

    
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"didReceiveData");
    [self.responseData appendData:data];
    //outputPost.text = [[NSString alloc] initWithData:self.responseData encoding:NSASCIIStringEncoding];
    NSString *result = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
    NSString *result1 = @"true";
    NSLog(@"%@", result);
    if([result isEqualToString:result1]) {
    NSLog(@"iitaasdad");
    //UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

        //[self presentViewController:home animated:YES completion:nil];
        [self performSegueWithIdentifier:@"home" sender:self];

    } else {
        NSLog(@"no");
    }

}

-(void) checkFields {
    if([_usernameField.text isEqualToString:@""] || [_emailField.text isEqualToString:@""] || [_passwordField.text isEqualToString:@""] || [_verifyPasswordField.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Ooppss" message:@"Please enter all fields" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil ];
        [alert show];
    } else {
        [self checkPasswords];
    }
}

-(void) checkPasswords {
    if(![_passwordField.text isEqualToString:_verifyPasswordField.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Ooppss" message:@"Passwords don't match" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil ];
        [alert show];
    }
}
@end
